package com.noble.glamour.bean.config;

import com.noble.glamour.bean.config.base.Action;
import com.noble.glamour.bean.config.base.Common;
import com.noble.glamour.bean.config.base.Gift;
import com.noble.glamour.bean.config.base.H5;
import com.noble.glamour.bean.config.base.Lite;
import com.noble.glamour.bean.config.base.NobilityList;
import com.noble.glamour.bean.config.base.Pay;
import com.noble.glamour.bean.config.base.Video;
import com.noble.library.base.bean.BaseData;
import com.noble.glamour.module.util.WebAppDownloader;

import org.json.JSONObject;

import java.util.List;

/**
 * 在线配置
 * Created by ZRP on 2017/4/14.
 */
public class CommonConfig extends BaseData {

    public static final String STATIC_CONFIG_FILE = "static_config.json";

    private Lite lite;                  // 轻量包升级全量包配置
    private Common common;              // 一些杂乱无法归类的配置
    private Gift gift;                  // 发送礼物选择列表，根据不同场景选择不同的列表
    private H5 h5;                      // h5资源文件下载配置
    private NobilityList nobilityList;  // 爵位信息配表
    private Pay pay;                    // 支付信息配置
    private Video video;                // 视频信息配置
    private Action action;              // 活动推送配置

    @Override
    public void parseJson(String jsonStr) {
        JSONObject jsonObject = getJsonObject(getJsonObject(jsonStr).optString("res"));

        lite = new Lite();
        lite.parseJson(jsonObject.optString("android_config"));

        common = new Common();
        common.parseJson(jsonObject.optString("common_config"));

        gift = new Gift();
        gift.parseJson(jsonObject.optString("gift_config"));

        h5 = new H5();
        h5.parseJson(jsonObject.optString("h5_config"));

        nobilityList = new NobilityList();
        nobilityList.parseJson(jsonObject.optString("nobility_config"));

        pay = new Pay();
        pay.parseJson(jsonObject.optString("pay_config"));

        video = new Video();
        video.parseJson(jsonObject.optString("video_config"));

        action = new Action();
        action.parseJson(jsonObject.optString("web_activity_config"));

        // 下载应用内H5
        WebAppDownloader.getInstance().download(h5.getWebappUrl(), h5.getWebappVersion());

        // 下载视频内H5
        WebAppDownloader.getInstance().downloadVideoH5(h5.getVideoUrl(), h5.getVideoVersion());
    }

    public Lite getLite() {
        return lite == null ? new Lite() : lite;
    }

    public Common getCommon() {
        return common == null ? new Common() : common;
    }

    public Gift getGift() {
        return gift == null ? new Gift() : gift;
    }

    public H5 getH5() {
        return h5 == null ? new H5() : h5;
    }

    public NobilityList getNobilityList() {
        return nobilityList == null ? new NobilityList() : nobilityList;
    }

    public Pay getPay() {
        return pay == null ? new Pay() : pay;
    }

    public Video getVideo() {
        return video == null ? new Video() : video;
    }

    public Action getAction() {
        return action == null ? new Action() : action;
    }

    // ----------------外层获取内层方法------------------

    public List<NobilityList.Nobility> getNobilitys() {
        return getNobilityList().getNobilityList();
    }

    public NobilityList.Nobility queryNobility(int id) {
        return getNobilityList().queryNobility(id);
    }

    @Override
    public String toString() {
        return "CommonConfig{" +
                "lite=" + lite +
                ", common=" + common +
                ", gift=" + gift +
                ", h5=" + h5 +
                ", nobilityList=" + nobilityList +
                ", pay=" + pay +
                ", video=" + video +
                ", action=" + action +
                '}';
    }
}
